import http,{ type AxiosResponse, type InternalAxiosRequestConfig , } from 'axios'
import { ResponseStatusHandler } from './auth';
import router from '@/router/index';


// const process = import.meta;//等同vue2中process.env.xxxx


//不需要弹出报错响应的url (由各自组件内处理)
const noResponseStatusErrorMsgUrl = ["/auth/do-login"];

// 创建 axios 实例
const Request = http.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: import.meta.env.VITE_APP_SERVER_TIME_OUT || 60000
});

//请求拦截
Request.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  console.warn(config)
    return config;
  },
  (error: unknown) => {
    return Promise.reject(error);
  }
);
//响应拦截
Request.interceptors.response.use((config: AxiosResponse) => {

  if(noResponseStatusErrorMsgUrl.indexOf(config.config.url as string) == -1){
    ResponseStatusHandler(config);
  }
  //认证失败
  if(config.data.code === 401){
    router.replace({ path: import.meta.env.VITE_APP_LOGIN_PATH || '/'})
  }
  return config.data;
  },
  (error: unknown) => {
    return Promise.reject(error);
  }
);

export default Request;
